package com.wandian.cqcsk.mvvm.checkfile;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.glide.GlideApp;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.MyScreenUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.databinding.ActivityCheckFileBinding;

import me.jessyan.autosize.utils.ScreenUtils;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 产看文件
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_CHECK_FILE)
public class CheckFileActivity extends BaseMVVMActivity<CheckFileViewModel> {
    private ActivityCheckFileBinding dataBinding;
    private String file_type;
    private String path;
    private int pic_width = 360;
    private int pic_height = 680;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_check_file);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        RelativeLayout.LayoutParams viewSys =(RelativeLayout.LayoutParams) dataBinding.viewSys.getLayoutParams();
        viewSys.height = ScreenUtils.getStatusBarHeight();
        dataBinding.viewSys.setLayoutParams(viewSys);
        pic_width =  MyScreenUtils.getScreenWidth(CheckFileActivity.this)- UIUtils.dp2px(60);
        pic_height =  MyScreenUtils.getScreenHeight(CheckFileActivity.this)- UIUtils.dp2px(60)- ScreenUtils.getStatusBarHeight();
        if(pic_width<360){
            pic_width = 360;
        }
        if(pic_height<680){
            pic_height = 680;
        }
        Bundle bundle = getIntent().getExtras();
        file_type = bundle.getString("file_type")+"";
        path = bundle.getString("file_path");
        initViewData();
        initClink();
    }
    private void initViewData(){

        dataBinding.rlFile.setVisibility(View.VISIBLE);
        dataBinding.tvToolTitle.setText("照片");
        dataBinding.ivPic.setVisibility(View.VISIBLE);

        //获取图片真正的宽高
        GlideApp.with(mContext)
                .asBitmap()
                .load(path)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int width = resource.getWidth();
                        int height = resource.getHeight();
                        //  Log.d("SimpleTarget",path+"="+width+"="+height+"="+pic_width+"="+pic_height);
                        if(width>=height && height>0 && width>0){
                            GlideUtil.showImageWidthRatio(CheckFileActivity.this,path,dataBinding.ivPic,pic_width);
                        }else {
                            GlideUtil.showImageHeightRatio(CheckFileActivity.this,path,dataBinding.ivPic,pic_height);
                        }
                    }
                });
    }

    private void initClink(){
        dataBinding.ivBack.setOnClickListener(v -> finish());
    }



}
